package com.criticalbotch.ijnition;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

import org.json.JSONObject;

import com.criticalbotch.ijnition.checkers.TagChecker;
import com.google.gson.Gson;

public class TagManager {

	private HashMap<Integer, EnjinTag> tags = new HashMap<Integer, EnjinTag>();

	private long timeLastChecked;
	private int checkPeriod = 300000; // 5 minutes
	private iJnition core;

	public TagManager() {
	}

	public TagManager(iJnition core) {
		setCore(core);
		checkTags();
	}

	public TagManager(iJnition core, int checkPeriod) {
		setCore(core);
		setCheckPeriod(checkPeriod);
		checkTags();
	}

	private void checkTags() {
		TagChecker checker = new TagChecker(getCore().getBaseURL()
				+ "/api/get-tags", this, getCheckPeriod());
		checker.start();
		setTimeLastChecked(System.currentTimeMillis());
	}

	private int getCheckPeriod() {
		return checkPeriod;
	}

	private iJnition getCore() {
		return core;
	}

	public HashMap<Integer, EnjinTag> getTags() {
		return tags;
	}

	public EnjinTag getTag(String name) {
		EnjinTag tag = null;

		HashMap<Integer, EnjinTag> tagList = tagsFromJSONFile("get-tags.json");

		for (int id : tagList.keySet())
			if (name.equals(tagList.get(id).getName()))
				tag = tagList.get(id);

		return tag;
	}

	public EnjinTag getTag(int ID) {

		EnjinTag tag = null;

		HashMap<Integer, EnjinTag> tagList = tagsFromJSONFile("get-tags.json");

		if (tagList.containsKey(ID))
			tag = tagList.get(ID);

		return tag;

	}

	void loadTags() {
		setTags(tagsFromJSONFile(core.getBaseURL() + "/api/get-tags"));
	}

	public void notifiedTagCheck() {
		checkTags();
	}

	public void setCheckPeriod(int checkPeriod) {
		this.checkPeriod = checkPeriod;
	}

	private void setCore(iJnition core) {
		this.core = core;
	}

	public void setTags(HashMap<Integer, EnjinTag> tags) {
		this.tags = tags;
	}

	private void setTimeLastChecked(long timeLastChecked) {
		this.timeLastChecked = timeLastChecked;
	}

	public HashMap<Integer, EnjinTag> tagsFromJSONFile(String f) {
		return tagsFromJSONFile(new File(f));
	}

	public HashMap<Integer, EnjinTag> tagsFromJSONFile(File f) {

		HashMap<Integer, EnjinTag> tagList = new HashMap<Integer, EnjinTag>();

		try {
			Gson gson = new Gson();

			BufferedReader reader = new BufferedReader(new FileReader(f));

			String json = "";
			String line;

			while ((line = reader.readLine()) != null)
				json += line;

			reader.close();

			JSONObject job = new JSONObject(json);

			job = job.optJSONObject("tags"); // pull only the tags

			for (Object id : job.keySet()) {
				String ID = id.toString();
				int tagID = Integer.parseInt(ID);
				EnjinTag tag = gson.fromJson(job.get(ID).toString(),
						EnjinTag.class);
				tag.setID(tagID);
				tagList.put(tagID, tag);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tagList;
	}

}
